package com.clps.rulesregulations.controller;

import com.clps.rulesregulations.domain.AccessFile;
import com.clps.rulesregulations.domain.Role;
import com.clps.rulesregulations.form.RoleResourceForm;
import com.clps.rulesregulations.service.ResourceService;
import com.clps.rulesregulations.service.RoleResourceService;
import com.clps.rulesregulations.service.RoleService;
import com.clps.rulesregulations.util.ResourceTree;
import com.clps.rulesregulations.util.ResultView;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 * @author zhangchao
 * @date 2018-10-08
 */
@Controller
@RequestMapping(value = "/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private RoleResourceService roleResourceService;

    /**
     * 跳转到角色列表页
     *
     * @return
     */
    @GetMapping("/list")
    public String toList() {
        return "manager/role";
    }

    /**
     * 查询角色列表
     *
     * @param condition
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public ResultView list(@RequestBody Map<String, Object> condition) {
        ResultView resultView = new ResultView(ResultView.SUCCESS);
        PageInfo<Role> roles = roleService.findRolesByPage(condition);
        resultView.setValue(roles);
        return resultView;
    }

    /**
     * 查询访问记录
     *
     * @param condition
     * @return
     */
    @PostMapping("/accessfilelist")
    @ResponseBody
    public ResultView accessfilelist(@RequestBody Map<String, Object> condition) {
        ResultView resultView = new ResultView(ResultView.SUCCESS);
        PageInfo<AccessFile> accessLogs = roleService.findAccessFileLogByPage(condition);
        List list = accessLogs.getList();
        if(list!=null && list.size()>0){
            AccessFile ac = (AccessFile) list.get(0);
            String fileName = ac.getFileName();
            resultView.setMsg(fileName);
        }
        resultView.setValue(accessLogs);
        return resultView;
    }

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @PostMapping("/add")
    @CacheEvict(cacheNames = "roles", allEntries = true)
    @ResponseBody
    public ResultView add(@RequestBody Role role) {
        ResultView resultView = new ResultView(ResultView.SUCCESS);
        if (role == null || StringUtils.isBlank(role.getRoleName())) {
            resultView.setStatus(ResultView.ERROR);
            resultView.setMsg("请输入角色名称");
            return resultView;
        }
        roleService.addRole(role);
        return resultView;
    }

    /**
     * 获取角色信息
     *
     * @param roleId
     * @return
     */
    @GetMapping("/edit")
    @ResponseBody
    public ResultView toEdit(@RequestParam Integer roleId) {
        ResultView resultView = new ResultView(ResultView.SUCCESS);
        Role role = roleService.findRoleById(roleId);
        resultView.setValue(role);
        return resultView;
    }

    /**
     * 更新角色信息
     *
     * @param role
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    @CacheEvict(cacheNames = "roles", allEntries = true)
    public ResultView edit(@RequestBody Role role) {
        ResultView resultView = new ResultView(ResultView.SUCCESS);
        roleService.updateRole(role);
        return resultView;
    }

    /**
     * 删除角色信息
     *
     * @param roleId
     * @return
     */
    @GetMapping("/delete")
    @CacheEvict(cacheNames = "roles", allEntries = true)
    @ResponseBody
    public ResultView delRole(@RequestParam Integer roleId) {
        ResultView resultView = new ResultView(ResultView.SUCCESS);
        if (roleId == null) {
            resultView.setStatus(ResultView.ERROR);
            resultView.setMsg("请选择角色ID");
            return resultView;
        }
        roleService.delRoleByRoleId(roleId);

        return resultView;
    }

    /**
     * 根据角色ID查询所有资源
     *
     * @param roleId
     * @return
     */
    @GetMapping("/resource/edit")
    @ResponseBody
    public ResultView findRoleResource(@RequestParam Integer roleId) {
        ResultView resultView = new ResultView(ResultView.SUCCESS);
        //查询所有资源
        List<ResourceTree> resources = resourceService.findAllAsTree();
        if (resources != null & !resources.isEmpty()) {
            resources = resources.get(0).getChildren();
        }
        //查询该角色所有资源ID
        List<Integer> resourceIds = roleResourceService.findResourceIdsByRoleId(roleId);
        Map<String, Object> map = new HashMap<>();
        map.put("resources", resources);
        map.put("resourceIds", resourceIds);
        resultView.setValue(map);
        return resultView;
    }

    /**
     * 更新角色资源
     *
     * @param roleResourceForm
     * @return
     */
    @PostMapping("/resource/edit")
    @ResponseBody
    @CacheEvict(cacheNames = "resources", allEntries = true)
    public ResultView updateRoleResource(@RequestBody RoleResourceForm roleResourceForm) {
        ResultView resultView = new ResultView(ResultView.SUCCESS);
        roleResourceService.updateRoleResource(roleResourceForm.getRoleId(), roleResourceForm.getResourceIds());
        return resultView;
    }
}
